/**
 * 
 */
package qjs.com.vn.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import qjs.com.vn.model.BaoBi;
import qjs.com.vn.model.Ncc;
import qjs.com.vn.service.BaoBiService;
import qjs.com.vn.service.NccService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author WIN7
 *
 */
public class BaoBiAction extends ActionSupport implements ModelDriven<Object> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -840093052470599037L;
   
	BaoBi baobi = new BaoBi();
	BaoBi baoBiNew = new BaoBi();
	BaoBi baobiEdit =  new BaoBi();
	
	List<Ncc> listNccSelected;
	List<BaoBi> listBBSelected;
	
	BaoBiService baoBiService;
	NccService nccService;
	List<BaoBi> listBBSearch;
	
	public List<Ncc> getListNccSelected() {
		return listNccSelected;
	}

	public void setListNccSelected(List<Ncc> listNccSelected) {
		this.listNccSelected = listNccSelected;
	}

	public List<BaoBi> getListBB() {
		return listBBSearch;
	}
	
	public void setListBB(List<BaoBi> listBBSearch) {
		this.listBBSearch = listBBSearch;
	}
	
	public List<BaoBi> getListBBSelected() {
		return listBBSelected;
	}

	public void setListBBSelected(List<BaoBi> listBBSelected) {
		this.listBBSelected = listBBSelected;
	}

	public List<BaoBi> getListBBSearch() {
		return listBBSearch;
	}

	public void setListBBSearch(List<BaoBi> listBBSearch) {
		this.listBBSearch = listBBSearch;
	}

	public void setNccService(NccService nccService) {
		this.nccService = nccService;
	}

	public void setBaoBiService(BaoBiService baoBiService) {
		this.baoBiService = baoBiService;
	}
	
	public String AddBaoBi()
	{
		baobi.setCreatedby("User created");
		baobi.setCreateddate(new Date());
		baoBiService.addBaoBi(baobi);
		return SUCCESS;
	}
	public String updateBaoBi()
	{
		baobi.setUpdatedby("User updated");
		baobi.setUpdateddate(new Date());
		baoBiService.editBaoBi(baobi);
		return SUCCESS;
	}
	public String deleteBaoBi()
	{
		baoBiService.editBaoBi(baobi);
		return SUCCESS;
	}
	public List<BaoBi> getListBaoBi()
	{
		listBBSelected = new ArrayList<BaoBi>();
		listBBSelected = baoBiService.listBaoBi();
		return listBBSelected;
	}
	
	public List<BaoBi> loadListBaoBi()
	{
		listBBSearch = new ArrayList<BaoBi>();
		listBBSearch = baoBiService.listBaoBi();
		return listBBSearch;
	}
	public String searchBaoBi()
	{
		listBBSearch = new ArrayList<BaoBi>();
		listBBSearch = baoBiService.searchBaoBi(baobi.getMabaobi(), baobi.getTenbaobi(), baobi.getMancc());
		return SUCCESS;
	}
	public List<Ncc> getListNcc()
	{
		listNccSelected = new ArrayList<Ncc>();
		listNccSelected = nccService.listNcc();
		return listNccSelected;
	}
	
	public BaoBi getOneBaoBiAction()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);		
		//baobi = baoBiService.getOneBaoBi(request.getParameter("baobiid"));
		baobiEdit = baoBiService.getOneBaoBi(request.getParameter("baobiid"));
		return baobiEdit;
	}
	public String getNameNccAction()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get( ServletActionContext.HTTP_REQUEST);
		Ncc ncc = new Ncc();
		ncc = nccService.getOneNcc(request.getParameter("mancc"));
		baobi.setTenncc(ncc.getTenncc());
		return SUCCESS;
		
	}

	public String execute() throws Exception {
		getListBaoBi();
		loadListBaoBi();
		getListNcc();
		return INPUT;
	}
	public String loadPopupToAdd()
	{
		return INPUT;
	}
	public String loadPopupToEdit()
	{
		getOneBaoBiAction();
		return INPUT;
	}
	public BaoBiAction()
	{
	
	}	
	@Override
	public Object getModel() {
		return baobi;
	}
	
	public BaoBi getBaobiEdit() {
		return baobiEdit;
	}
}
